import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { NiucloudService } from '../../services/admin/niucloud.service';
import { CoreCloudBuildService } from '../../services/core/core-cloud-build.service';

/**
 * CloudController
 * 对应 PHP: Cloud Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('niucloud')
@Controller('adminapi/niucloud')
export class CloudController {
  constructor(
    private readonly niucloudService: NiucloudService,
    private readonly coreCloudBuildService: CoreCloudBuildService,
  ) {}

  /**
   * 云编译
   * 路由: POST build
   * PHP路由: Route::post('build', 'niucloud.Cloud/build')
   */
  @Post('build')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async build(@Body() data: BuildDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: build

      return await this.coreCloudBuildService.cloudBuild();
    } catch (error) {
      throw new BusinessException('build操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: GET build/log
   * PHP路由: Route::get('build/log', 'niucloud.Cloud/getBuildLog')
   */
  @Get('build/log')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async getBuildLog(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getBuildLog

      return await this.coreCloudBuildService.getBuildLog();
    } catch (error) {
      throw new BusinessException('getBuildLog操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: GET build
   * PHP路由: Route::get('build', 'niucloud.Cloud/getBuildTask')
   */
  @Get('build')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async getBuildTask(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getBuildTask

      return await this.coreCloudBuildService.getBuildTask();
    } catch (error) {
      throw new BusinessException('getBuildTask操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: POST build/clear
   * PHP路由: Route::post('build/clear', 'niucloud.Cloud/clearBuildTask')
   */
  @Post('build/clear')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async clearBuildTask(@Body() data: ClearBuildTaskDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: clearBuildTask

      return await this.coreCloudBuildService.clearTask();
    } catch (error) {
      throw new BusinessException('clearBuildTask操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: GET build/check
   * PHP路由: Route::get('build/check', 'niucloud.Cloud/buildPreCheck')
   */
  @Get('build/check')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async buildPreCheck(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: buildPreCheck

      return await this.coreCloudBuildService.buildPreCheck();
    } catch (error) {
      throw new BusinessException('buildPreCheck操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: POST build/connect_test
   * PHP路由: Route::post('build/connect_test', 'niucloud.Cloud/connectTest')
   */
  @Post('build/connect_test')
  @ApiOperation({ summary: '云编译' })
  async connectTest(@Body() data: ConnectTestDto) {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: connectTest
      // 服务方法名解析失败，需要手动实现
      return { message: 'connectTest - 服务方法名解析失败，需要手动实现' };
    } catch (error) {
      throw new BusinessException('connectTest操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: POST build/set_local_url
   * PHP路由: Route::post('build/set_local_url', 'niucloud.Cloud/setLocalCloudCompileConfig')
   */
  @Post('build/set_local_url')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async setLocalCloudCompileConfig(
    @Body() data: SetLocalCloudCompileConfigDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: setLocalCloudCompileConfig

      return await this.niucloudService.setLocalCloudCompileConfig(data);
    } catch (error) {
      throw new BusinessException('setLocalCloudCompileConfig操作失败', error);
    }
  }

  /**
   * 云编译
   * 路由: GET build/get_local_url
   * PHP路由: Route::get('build/get_local_url', 'niucloud.Cloud/getLocalCloudCompileConfig')
   */
  @Get('build/get_local_url')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '云编译' })
  async getLocalCloudCompileConfig(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getLocalCloudCompileConfig

      return await this.niucloudService.getLocalCloudCompileConfig();
    } catch (error) {
      throw new BusinessException('getLocalCloudCompileConfig操作失败', error);
    }
  }
}
